home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6113 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: changing strings via pointers
  5. Date: 22 Feb 1996 19:18:32 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4gifi8$pau@sparcserver.lrz-muenchen.de>
  9. References: <1996Feb22.125436.25503@leeds.ac.uk>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. csyamc@scs.leeds.ac.uk (A M Casey) writes:
  13.  
  14. >I reading in strings from a file using fgets, which stores the "\n" in
  15. >the array of chars which I have declared using a pointer.
  16.  
  17. >The thing is, I want to get rid of the "\n" and replace it with "\0", but
  18. >obviously I can only do this using the pointer. I'm using the following code:
  19.  
  20. First, let me suggest a "simple" solution for your problem. As in
  21. most cases, the standard C library is your friend. Have a look at
  22. strlen() and/or strchr().
  23.  
  24. >char *Contents < pointer to string
  25.  
  26. >int count = 0;
  27.  
  28.  
  29. >while (*(Contents + count)!= NULL)  /* the end of string is not reached */
  30.  
  31. I'd rather write this as
  32.  
  33.  while (Contents[count] != '\0')
  34.  
  35. >{if (*(Contents + count) == atoi("\n"))
  36.  
  37. I'd rather write this as
  38.  
  39.  if (Contents[count] == '\n')
  40.  
  41. and in this case, it is not just a stylistic issue. "atoi("\n")" is 
  42. certainly not what you want in this situation. It simply does not do
  43. what you probably want. "atoi("13")" might work on a lot of systems,
  44. though.
  45.  
  46. >/* change the \n to a \0 and set the next char along to null */
  47. >{
  48. >*(Contents + count) = atoi("\0");
  49.  
  50. Again, I'd rewrite this as
  51.  
  52.    Contents[count] = '\0';
  53.  
  54. Your version will work, but maybe for reasons different from what
  55. you seem to think.
  56.  
  57. >*(Contents + count + 1) = NULL;
  58.  
  59. The string _is_ terminated by a '\0', and there is no need to
  60. terminate it by _two_ '\0' s
  61.  
  62. >} 
  63. >else
  64. >/* read in the next char */
  65. >{
  66. >count++;
  67.  
  68. >I think the problem maybe my way of using the pointer - I can print out each
  69. >char as I get to it, but it doesnt seem to change the array at all.
  70. >Should I be using atoi?, it doesnt seem to work without it.
  71.  
  72. 1) You should not use atoi(), at least not the way you do.
  73.  
  74. 2) The error messages you got before you inserted atoi() might
  75.    have pointed you in the right direction.
  76.  
  77. 3) 'a' is an int in C. It is called a character constant.
  78.  
  79. Kurt
  80. --
  81. | Kurt Watzka                             Phone : +49-89-2180-6254
  82. | watzka@stat.uni-muenchen.de
  83. | ua302aa@sunmail.lrz-muenchen.de
  84.